package com.manlitech.cloud.stream.componet.rocketmq;

/**
 * 新建消息生产端 Producer，负责生产消息
 * @author shensg
 * @date 2021/7/29 15:47
 */
import com.manlitech.cloud.stream.config.rocketmq.RmqConfig;
import lombok.Getter;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Getter
@Component
public class Producer {

    @Autowired
    RmqConfig rmqConfig;

    private DefaultMQProducer producer;

    @PostConstruct
    private void producerInit() throws MQClientException {
        producer = new DefaultMQProducer(rmqConfig.PRODUCER_GROUP);
        // 不开启 vip 通道 开通口端口会减 2
        producer.setVipChannelEnabled(false);
        // 绑定 name server
        producer.setNamesrvAddr(rmqConfig.NAME_SERVER);
        producer.start();
    }
}